﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgNetComponents.Classes;

namespace Wydatki.Setup.Tools
{
    public partial class ProduktMatch : Form
    {
        public static event ProductMatchHandler ProductMatched;
        public static void OnProductMatched(int oldP, int newP)
        {
            if (ProductMatched != null)
                ProductMatched(new ProductMatchEventArgs(oldP, newP));
        }

        public ProduktMatch()
        {
            InitializeComponent();
        }
        private bool ValidateAll()
        {
            produktMatchDataSet.PRODUKT_MATCH.ClearErrors();
            string msg = "";
            bool res =  modeProvider1.ValidateRequiredColumns(ref msg);
            if (res)
            {
                res = produktMatchDataSet.PRODUKT_MATCH.ValidateColUniqueLng("NEW_VALUE", "ERR_VAL_UNIQUE");
            }
            return res;
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!ValidateAll())
                this.ShowDxLngWarningDialog("WARN_VALIDATION_FAILED");
            else
            {
                Dictionary<int,int> dic = new Dictionary<int, int>();

                DataRow[] rows = produktMatchDataSet.PRODUKT_MATCH.Select();
                bool result = true;
                string query = "UPDATE WRK_WYDATKI_PRODUKTY SET WRK_WYD_PROD_PRODUKT = @NEW_VALUE WHERE WRK_WYD_PROD_PRODUKT = @OLD_VALUE";
                int i;
                NamedObject NEW_VALUE = new NamedObject("@NEW_VALUE", null);
                NamedObject OLD_VALUE = new NamedObject("@OLD_VALUE", null);
                foreach (DataRow r in rows)
                {
                    NEW_VALUE.Value = r["NEW_VALUE"];
                    OLD_VALUE.Value = r["OLD_VALUE"];
                    dic.Add(OLD_VALUE.Value.AsInt(), NEW_VALUE.Value.AsInt());

                    i = sqlCeDataManager1.ExecuteNonQuery(query, NEW_VALUE, OLD_VALUE);
                    if (i < 0)
                    {
                        result = false;
                        break;
                    }
                }
                if (result)
                {
                    sqlCeDataManager1.Commit();
                    foreach (KeyValuePair<int,int>kvp in dic)
                    {
                        OnProductMatched(kvp.Key, kvp.Value);
                    }
                    ProgNetComponentsDx.Browsers.BrowserCache.RefreshCacheForTable("STP_PRODUKTY");

                    this.ShowDxInfoDialog("Produkty zostały uaktualnione.");
                    Close();
                }
                else
                {
                    sqlCeDataManager1.Rollback();
                    this.ShowDxErrorDialog("Błąd uaktualniania!");
                }
            }
        }


        public class ProductMatchEventArgs : EventArgs
        {
            public int OldProduct
            {
                get;
                private set;
            }

            public int NewProduct
            {
                get;
                private set;
            }
            public ProductMatchEventArgs(int oldProduct, int newProduct)
            {
                this.NewProduct = newProduct;
                this.OldProduct = oldProduct;
            }
        }
        public delegate void ProductMatchHandler(ProductMatchEventArgs e);
    }


}
